package com.bugsnag.ohos.util;

import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

/**
 * file utils
 *
 * @since 2021-07-05
 */
public class FileUtils {
    private static final int LENGTH = 8 * 1024;

    private FileUtils() {
    }

    /**
     * copy file
     *
     * @param bufferedReader
     * @param writer
     * @return copied chars
     */
    public static long copyTo(Reader bufferedReader, Writer writer) {
        long charsCopied = 0L;
        char[] charsBuffer = new char[LENGTH];
        int chars = 0;
        try {
            chars = bufferedReader.read(charsBuffer, 0, charsBuffer.length);
            while (chars >= 0) {
                writer.write(charsBuffer, 0, chars);
                charsCopied += chars;
                chars = bufferedReader.read(charsBuffer, 0, charsBuffer.length);
            }
        } catch (IOException e) {
            e.getMessage();
        }
        return charsCopied;
    }
}
